package vn.edu.saigontech.ncms.service.impl;

import java.util.Collection;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import vn.edu.saigontech.ncms.model.user.LoggingRecord;
import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.LoggingRecordService;
import vn.edu.saigontech.ncms.util.Checker;

@SuppressWarnings("unchecked")
public class LoggingRecordServiceImpl extends HibernateGenericService<LoggingRecord, Long> implements LoggingRecordService  {
	
	@Override
	public Collection<LoggingRecord> find(User user, Date fromDate,
			Date toDate, String action, String description) {
		Criteria criteria = this.createCriteria();
		if(Checker.isNotNull(user)) {
			criteria.add(Restrictions.eq("actor", user));
		}
		if(Checker.isNotNull(fromDate) && Checker.isNotNull(toDate)) {
			criteria.add(Restrictions.between("interactedDate", fromDate, toDate));
		} else {
			if(Checker.isNotNull(fromDate)) {
				criteria.add(Restrictions.ge("interactedDate", fromDate));
			}
			if(Checker.isNotNull(toDate)) {
				criteria.add(Restrictions.le("interactedDate", toDate));
			}
		}
		if(!Checker.isEmpty(action)) {
			criteria.add(Restrictions.eq("interactedAction", action));
		}
		if(!Checker.isEmpty(description)) {
			criteria.add(Restrictions.ilike("description", description, MatchMode.ANYWHERE));
		}
		return criteria.list();
	}
}